<?php
    function baiduwap($tasks){
        
        Global $whitelist; //白名单
        Global $posturl;
        Global $filepath;
        $nul=0;
        
        $header = getmobileheader();
        
        //每页搜索记录
        for($pn=0;$pn<$tasks['pn'];$pn++){
            
            $url="https://m.baidu.com/s?word=".str_replace(' ','%20',$tasks['search'])."&pn=".($pn*10);
            
            print_f($url."\t");
            
            $contents=Get_Web_Contents($url,'GET','','',$header);
            
            $text=str_replace(array("\r\n", "\r", "\n"), "",$contents['Body']);
            
            $text=preg_replace("/<(style|script)(.*?)<\/(style|script)>/i", "", $text);//去css和js
            
            preg_match_all('/(srcid="www_normal"|srcid="h5_mobile")(.*?)<\/span><\/div><\/div><\/div><\/div>/',$text,$content);//取搜索记录
            
            //每条搜索记录
            foreach($content[0] as $k=>$v){
                
                preg_match("/c-showurl\">([^\/]+\.)?({$whitelist})</",$v,$url);//取网站快照
                
                if(!empty($url)){//白名单
                    continue;
                }
                preg_match("/href=\"http(.*?)\"/",$v,$url);//取网站快照
                
                $url=html_entity_decode(urldecode("https".$url[1]));
                
                $finalsnapshot=Get_Web_Contents($url,'GET','','',$header);
                
                preg_match("/(href=\"|replace\(\")(.*?)\"/",$finalsnapshot['Body'],$url);
                
                $finalsnapshot=finalsnapshot($url[2],'GET','','',$header);
                
                if(empty($finalsnapshot['Body'])){
                    print_f("快照为空\n".$url[2]);
                    continue;
                }
                
                $snapshot=str_replace(array("\r\n", "\r", "\n"," ","\t"), "",$finalsnapshot['Body']);
                
                $snapshot=mb_convert_encoding($snapshot, 'UTF-8',mb_detect_encoding($snapshot, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')));//转码
                
                preg_match('/<title>(.*?)<\/title>/i',$snapshot,$data);
                
                $snapshot=codeWords($snapshot);//命中词加样式
                
                if(!empty($snapshot)){
                    $insert['tasks_id']    = $tasks['id'];
                    $insert['tasks_name']= $tasks['name'];
                    $insert['search']    = $tasks['search'];
                    $insert['keyword']    = $snapshot['count'];
                    $insert['title']    = $data[1];
                    $insert['url']    = $url[2];
                    $insert['pn']    = $pn+1;
                    $insert['status']    = 1;
                    $insert['addtime']    = time();
                    $insert['snapshot']    = $snapshot['snapshot'];
                    Get_Web_Contents($posturl,'POST',$insert);
                }
            }
            echo "\n";
        }
    }
?>
